package com.jiamao.goplay.ui.settings;

import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;
import android.content.res.Configuration;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProviders;

import com.gyf.immersionbar.ImmersionBar;
import com.jiamao.goplay.BR;
import com.jiamao.goplay.R;
import com.jiamao.goplay.app.AppApplication;
import com.jiamao.goplay.app.AppViewModelFactory;
import com.jiamao.goplay.base.BaseMVVMFragment;
import com.jiamao.goplay.config.Config;
import com.jiamao.goplay.databinding.SettingsLayoutBinding;
import com.jiamao.goplay.ui.login.LoginPhoneFragment;
import com.jiamao.goplay.ui.settings.vm.SettingsVM;
import com.jiamao.goplay.util.DataCleanManager;
import com.kongzue.dialog.interfaces.OnDialogButtonClickListener;
import com.kongzue.dialog.util.BaseDialog;
import com.kongzue.dialog.util.DialogSettings;
import com.kongzue.dialog.v3.MessageDialog;
import com.tamsiree.rxkit.view.RxToast;

import java.util.Objects;

import me.goldze.mvvmhabit.base.AppManager;
import me.goldze.mvvmhabit.utils.SPUtils;
import me.goldze.mvvmhabit.utils.ToastUtils;

public class SettingsFragment extends BaseMVVMFragment<SettingsLayoutBinding, SettingsVM> {
    @Override
    public int initContentView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return R.layout.settings_layout;
    }

    @Override
    public int initVariableId() {
        return BR.viewModel;
    }


    @Override
    public SettingsVM initViewModel() {
        //使用自定义的ViewModelFactory来创建ViewModel，如果不重写该方法，则默认会调用LoginViewModel(@NonNull Application application)构造方法
        AppViewModelFactory factory = AppViewModelFactory.getInstance(getActivity().getApplication());
        return ViewModelProviders.of(this, factory).get(SettingsVM.class);
    }

    @Override
    public void initViewObservable() {
        super.initViewObservable();

        viewModel.uc.logout.observe(this, new Observer<Boolean>() {
            @Override
            public void onChanged(@Nullable Boolean aBoolean) {
                //pSwitchObservable是boolean类型的观察者,所以可以直接使用它的值改变密码开关的图标
                showLogoutDialog();
            }
        });
        viewModel.uc.clearCache.observe(this, new Observer<Boolean>() {
            @Override
            public void onChanged(@Nullable Boolean aBoolean) {
                //pSwitchObservable是boolean类型的观察者,所以可以直接使用它的值改变密码开关的图标
                showcacheDialog();
            }
        });

        viewModel.uc.weChat.observe(this, new Observer<String>() {
            @Override
            public void onChanged(@Nullable String aBoolean) {
                //pSwitchObservable是boolean类型的观察者,所以可以直接使用它的值改变密码开关的图标
                copyContentToClipboard(aBoolean,getContext());
            }
        });

    }

    @Override
    public void initData() {
        super.initData();
        int uiModel = this.getResources().getConfiguration().uiMode & Configuration.UI_MODE_NIGHT_MASK;
        ImmersionBar.with(this). keyboardEnable(false).statusBarDarkFont(uiModel == Configuration.UI_MODE_NIGHT_YES?false:true).titleBar(binding.getRoot().findViewById(R.id.toolbar)).init();

        TextView titleTv =  binding.getRoot().findViewById(R.id.tvTitle);
         titleTv.setText("设置");
      binding.getRoot().findViewById(R.id.ll_back).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                    getActivity().finish();
            }
        });
    }


    void showLogoutDialog(){

        MessageDialog.build((AppCompatActivity) getContext())
                .setStyle(DialogSettings.STYLE.STYLE_IOS)
                .setTitle("是否退出登录")
                .setMessage(" ")
                .setOkButton("确定", new OnDialogButtonClickListener() {
                    @Override
                    public boolean onClick(BaseDialog baseDialog, View v) {

                        AppApplication.getInstance().startLogins();
                        return false;
                    }

                }).setCancelButton("取消")
                .show();

    }
    void showcacheDialog(){

        MessageDialog.build((AppCompatActivity) getContext())
                .setStyle(DialogSettings.STYLE.STYLE_IOS)
                .setTitle("是否清除缓存")
                .setMessage(" ")
                .setOkButton("确定", new OnDialogButtonClickListener() {
                    @Override
                    public boolean onClick(BaseDialog baseDialog, View v) {

                        //清除缓存
                        DataCleanManager.clearAllCache(Objects.requireNonNull(getContext()));
                        String clearSize = DataCleanManager.getTotalCacheSize(Objects.requireNonNull(getContext()));
                        viewModel.clearSize.set(clearSize);
                        return false;
                    }

                }).setCancelButton("取消")
                .show();

    }

    @Override
    public void onResume() {
        super.onResume();

        if (SPUtils.getInstance().getBoolean(Config.APP_SETINGS_TZ,true)){
           viewModel. openStatus.set("已开启");
        }else {
            viewModel.openStatus.set("未开启");
        }
    }


    /**
     * 复制内容到剪贴板
     *
     * @param content
     * @param context
     */
    public void copyContentToClipboard(String content, Context context) {
        //获取剪贴板管理器：
        ClipboardManager cm = (ClipboardManager) context.getSystemService(Context.CLIPBOARD_SERVICE);
        // 创建普通字符型ClipData
        ClipData mClipData = ClipData.newPlainText("Label", content);
        // 将ClipData内容放到系统剪贴板里。
        cm.setPrimaryClip(mClipData);
        RxToast.showToastShort("复制成功");
    }
}
